﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Management;
using System.Reflection;

using Consejo.Monitoring.BusinessObjects;
using Consejo.Monitoring.Destinations;

namespace WinMonitoringAgent
{
    public  class MonitorConsole
    {
        

        public void SendStatus()
        {
            
            List<IStatusItem> _statusItems;
            List<ServerStatus> _servers;
            SystemStatus _systemStatus;

            _servers = new List<ServerStatus>();
            _statusItems = new List<IStatusItem>();
            
           
            
            NameValueCollection SvcNvc = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection("AgentSettings/ServiceValues");
            for (int i = 0; i < SvcNvc.Count; i++)
            {
               
                string[] configurationValue = SvcNvc.Get(i).Split(new char[] { ',' });
                string datasourcetype = SvcNvc[0];
                string datasourcename = SvcNvc[1];
                string datasourcevalue = SvcNvc[2];
                string managementValue = null;
                Object[] returnType;
                string svcID = SvcNvc.GetKey(i);
                //string assem = ("Consejo.Monitoring.ApplicationHooks." + configurationValue[0]);
                //Type _helperClass = Consejo.Monitoring.ApplicationHooks.GetType("assem");
                if (datasourcename == "Ping")
                {
                    object returnValue = Consejo.Monitoring.ApplicationHooks.Values(datasourcetype,datasourcename,datasourcevalue, out  returnType);
                    bool serviceResult = returnValue[0];
                    double duration = returnValue[1];
                  _statusItems.Add(new ServiceStatus(svcID, serviceResult,duration));
                    //(string _serviceId,bool _serviceResult, double _duration)
                }
                else
                {
                    object returnValue = Consejo.Monitoring.ApplicationHooks.Values.GetManagementValue(datasourcetype,datasourcename,datasourcevalue, out  returnType);
                     bool serviceResult = returnType[0];
                     double duration = returnType[0];
                    _statusItems.Add(new ServiceStatus(svcID, returnType, 5));
                }
            }

            NameValueCollection ResourceNvc = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection("AgentSettings/ResourceValues");
            for (int i = 0; i < ResourceNvc.Count; i++)
            {
                string[] configurationValue = ResourceNvc.Get(i).Split(new char[] { ',' });
                string managementValue = null;
                string ResourceID = ResourceNvc.GetKey(i);
                IMonitorDestination _helperClass = new Type.GetType("Consejo.Monitoring.ApplicationHooks." + configurationValue[0]);
                object returnValue = _helperClass.GetManagementValue(configurationValue[1], out returnType);
                _statusItems.Add(new ResourceStatus(ResourceID, returnType));

            }
 


            NameValueCollection ServerNvc = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection("AgentSettings/ServerValues");
            string ServerID = Convert.ToString(ServerNvc["1"]);
            string AgentID = Convert.ToString(ServerNvc["AgentID"]);
            _servers.Add(new ServerStatus(ServerID, _statusItems));
             _systemStatus = new SystemStatus(_servers);
             string _serverUrl = System.Configuration.ConfigurationManager.AppSettings["PanoptaUrl"]+ AgentID;
             string userName = System.Configuration.ConfigurationManager.AppSettings["Username"];
             string password = System.Configuration.ConfigurationManager.AppSettings["Password"];
             string _monitorUrl = _serverUrl;
             StatusMonitor.SendStatus(_systemStatus, userName, password, _monitorUrl);
           
           
        }
    }
}